/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 38917
 * Date: 2025-02-10
 * Time: 03:12
 */
public class Queue {
    // 双向链表节点
    public static class ListNode {
        ListNode next;
        ListNode prev;
        int value;

        ListNode(int value) {
            this.value = value;
        }
    }

    ListNode first; // 队头
    ListNode last; // 队尾
    int size = 0;

    //入队列---向双向链表位置插入新节点,尾插法
    public void offer(int e) {
        ListNode newNode = new ListNode(e);
        if (first == null) {
            first = newNode;
        // last = newNode;
        } else {
            last.next = newNode;
            newNode.prev = last;
        // last = newNode;
        }
        last = newNode;
        size++;
    }
    //删除第一个元素
    public int poll(){
        int val = 0;
        if(first == null){
            System.out.println("链表为空");
            return -1;
        }else if (first == last){
            val = first.value;
            first = null;
            last = null;
            size--;
        }else{
            val = first.value;
            first = first.next;
            first.prev.next = null;
            first.prev = null;
            size--;
        }
        return val;
    }
    //获取队头元素---获取链表中第一个节点的值域
    public int peek(){
        if(first == null) {
            System.out.print("链表为空");
            return -1;
        }
        return first.value;
    }
    //size
    public int size(){
        return size;
    }
    //empty
    public Boolean isEmpty(){
        return first == null;
    }
    //display
    public void display(){
        if(first == null) {
            System.out.println("链表为空");
            return;
        }
        ListNode cur = first;
        while (cur != null){
            System.out.print(cur.value+" ");
            cur = cur.next;
        }
        System.out.println();
    }
}